package com.ithuang.quartz.task;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.PostConstruct;
import javax.mail.MessagingException;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Component;

import com.ithuang.quartz.utils.ImageUtil;

@Component("task1")
public class Task1 {

    @PostConstruct
    public void handle() throws InterruptedException, MessagingException, IOException {
        File nmp = new File("C:\\Users\\Administrator\\3D Objects\\新城阅璟台Y7-2-901柜体\\连线下料文件\\7304199361618_13_6.bmp");

        String img64 = ImageUtil.convertImageToBase64Str(nmp);
        String res = ImageUtil.shibieshuzi(img64);
        // res = res.replaceAll("诚成木业工厂", res);
        System.out.println(res);

        File xml = new File("C:\\Users\\Administrator\\3D Objects\\国补2025.2.28联发九州国际6-2402下单图\\定制国补联发九州国际6-2402下单图（自动贴标）\\连线下料文件\\List_对接设备.xml");
        
        String xmlstr = FileUtils.readFileToString(xml, StandardCharsets.UTF_8);
        
        String xmlreg = "PlateID\" Value=\"(.*nc)"; // 示例：匹配包含"error"的行
        Pattern xmlpattern = Pattern.compile(xmlreg);
        Matcher matcher = xmlpattern.matcher(xmlstr);
        
        // 如果行匹配正则表达式
        while (matcher.find()) {
        // if(matcher.find()){
            // for (int i = 0; i < matcher.groupCount(); i++) {
                System.out.println("    " + matcher.group(1));
            // }
            
        }


        String regexPattern = ".*error.*"; // 示例：匹配包含"error"的行
        
            // 创建 Pattern 对象
            Pattern pattern = Pattern.compile(regexPattern);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Date d = new Date();
        String s = sf.format(d);
        BufferedWriter writer = new BufferedWriter(new FileWriter(new File("C://"+s+"_log.txt")));
        File f = new File(s+".log");
        BufferedReader bf = new BufferedReader(new FileReader(f));
        String line = bf.readLine();
        // 逐行读取文件
        while ((line = bf.readLine()) != null) {
            // 创建 matcher 对象
            Matcher matcher2 = pattern.matcher(line);
            
            // 如果行匹配正则表达式
            if (matcher2.matches()) {
                // 写入到输出文件
                writer.write(line);
                writer.newLine(); // 写入换行符
            }
        }

    }

}
